package controllers

import (
	"backend/models"
	"backend/services"
	"net/http"

	"github.com/gin-gonic/gin"
)

type MenuController struct {
	service *services.MenuService
}

type MenuResp struct {
	Id         uint   `json:"id"`
	Name       string `json:"name"`
	Path       string `json:"path"`
	Icon       string `json:"icon"`
	CreatedAt  string `json:"created_at"`
	UpdatedAt  string `json:"updated_at"`
	ParentId   *uint  `json:"parent_id"`
	ParentName string `json:"parent_name"`
	OrderNum   uint   `json:"order_num"`
	Level      uint   `json:"level"`
}

type MenuPermission struct {
	Id         uint             `json:"id"`
	Name       string           `json:"name"`
	Children   []MenuPermission `json:"children"`
	Permission []struct {
		Id   uint
		Name string
	} `json:"permission"`
}

func NewMenuController(s *services.MenuService) *MenuController {
	return &MenuController{service: s}
}

func (m *MenuController) Create(c *gin.Context) {
	var menu models.Menu
	if err := c.ShouldBindJSON(&menu); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	if err := m.service.Create(&menu); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, menu)
}

func (m *MenuController) Update(c *gin.Context) {
	var menu models.Menu
	if err := c.ShouldBindJSON(&menu); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	if err := m.service.Update(&menu); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
	})
}

func (m *MenuController) Delete(c *gin.Context) {
	id, err := models.GetParamsInt(c, "id")
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"err": err.Error()})
		return
	}
	if err := m.service.Delete(uint(id)); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"err": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{"message": "success"})
}

func (m *MenuController) GetMenuList(c *gin.Context) {
	pageinfo, err := models.GetPageInfo(c)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"err": err.Error()})
		return
	}
	//name
	name := c.Query("name")
	path := c.Query("path")

	menus, total, err := m.service.GetMenuList(pageinfo, &models.Menu{Name: name, Path: path})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"err": err.Error()})
		return
	}

	resp := []MenuResp{}
	for _, item := range menus {
		parentID := uint(0) // Default value if parent is nil
		parentName := ""    // Default value if parent is nil

		if item.ParentID != nil {
			parentID = *item.ParentID
		}
		if item.Parent != nil {
			parentName = item.Parent.Name
		}
		resp = append(resp, MenuResp{
			Name:       item.Name,
			Path:       item.Path,
			Icon:       item.Icon,
			Id:         item.ID,
			ParentId:   &parentID,
			ParentName: parentName,
			OrderNum:   item.OrderNum,
			Level:      item.Level,
		})
	}
	c.JSON(http.StatusOK, gin.H{"data": resp, "total": total})

}
